package com.wsseieiei.fireballgame.items;

import com.wsseieiei.fireballgame.Main;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.MinecraftForge;

public class ItemSlimePlate extends Item {
    public ItemSlimePlate(Properties p_41383_) {
        super(p_41383_);
        MinecraftForge.EVENT_BUS.register(this);
    }

    public ItemSlimePlate() {
        super(new Properties().tab(Main.FIREBALL_GAME_TAB).stacksTo(1));
    }

    protected ItemStack findAmmo(Player player) {
        if (this.isSlimePlate(player.getItemInHand(InteractionHand.OFF_HAND))) {
            return player.getItemInHand(InteractionHand.OFF_HAND);
        } else if (this.isSlimePlate(player.getItemInHand(InteractionHand.MAIN_HAND))) {
            return player.getItemInHand(InteractionHand.MAIN_HAND);
        } else {
            for (int i = 0; i < player.getInventory().getContainerSize(); ++i) {
                ItemStack itemstack = player.getInventory().getItem(i);
                if (this.isSlimePlate(itemstack)) {
                    return itemstack;
                }
            }
            return ItemStack.EMPTY;
        }
    }

    protected boolean isSlimePlate(ItemStack stack) {
        return stack.getItem() instanceof ItemSlimePlate;
    }

    public InteractionResultHolder<ItemStack> use(Level level, Player playerIn, InteractionHand handIn) {
        ItemStack itemstack = playerIn.getItemInHand(handIn);
        boolean flag = !this.findAmmo(playerIn).isEmpty();
        InteractionResultHolder<ItemStack> ret = net.minecraftforge.event.ForgeEventFactory.onArrowNock(itemstack, level, playerIn, handIn, flag);
        if (ret != null) return ret;
        level.playSound((Player)null, playerIn.getX(), playerIn.getY(), playerIn.getZ(), SoundEvents.SLIME_SQUISH, SoundSource.NEUTRAL, 0.5F, 0.4F / (level.getRandom().nextFloat() * 0.4F + 0.8F));
        if (!playerIn.getAbilities().instabuild && !flag) {
            return InteractionResultHolder.fail(itemstack);
        } else {
            playerIn.startUsingItem(handIn);
            playerIn.addEffect(new MobEffectInstance(MobEffects.JUMP, 60,9,true,true));
            return InteractionResultHolder.consume(itemstack);
        }
    }

}
